import json

import pymysql
from flask import Blueprint, request, jsonify
from . import trailer_bp
from apps.trailer.trailerService import update_trailer_status_service, get_trailer_status_service, get_trailer_service, \
    get_trailer_name_service, contract_with_heartbeat_service, get_all_trailer_id
from ..gpsAndPathUtils import deal_cors

pymysql.install_as_MySQLdb()

@trailer_bp.route('/trailer/heartBeat', methods=["POST"])
def contract_with_heartbeat():
    data = json.loads(request.get_data())
    return contract_with_heartbeat_service(data)


@trailer_bp.route('/trailer/getStatus', methods=["GET"])
def get_trailer_status():
    """
    获取拖车状态的api
    ---
    tags:
      - 有关拖车的API
    parameters:
      - name: trailer_ID
        in: GET
        type: number string
        required: true
        description: 拖车ID
    responses:
      500:
        description: 未查找到相关ID的拖车
      200:
        description: 查询成功
        schema:
          id: 返回用例
          properties:
            code:
              type: number
              description: 状态号
              default: 200
            data:
              type: array
              description: 模糊查询拖车ID所返回的列表
              items:
                type: string
              default: ["1":0, "11":1]
    """
    trailer_ID = request.args.get("trailer_ID")
    print(trailer_ID)
    return get_trailer_status_service(request.args.get("trailer_ID"))


@trailer_bp.route('/trailer/getTrailer', methods=["GET"])
def get_trailer():
    trailer_ID = request.args.get("trailer_ID")
    """
    获取拖车状态的api
    ---
    tags:
      - 有关拖车的API
    parameters:
      - name: trailer_ID
        in: GET
        type: number string
        required: true
        description: 拖车ID
    responses:
      500:
        description: 未查找到相关ID的拖车
      200:
        description: 查询成功
        schema:
          id: 返回用例
          properties:
            code:
              type: number
              description: 状态号
              default: 200
            data:
              type: array
              description: 模糊查询拖车ID所返回的列表
              items:
                type: string
              default: ["1":0, "11":1]
    """

    return get_trailer_service(trailer_ID)


@trailer_bp.route('/trailer/getTrailers', methods=["GET"])
def get_trailers():
    trailer_ID = request.args.get("trailer_ID")

    """
    获取拖车状态的api
    ---
    tags:
      - 有关拖车的API
    parameters:
      - name: trailer_ID
        in: GET
        type: number string
        required: true
        description: 拖车ID
    responses:
      500:
        description: 未查找到相关ID的拖车
      200:
        description: 查询成功
        schema:
          id: 返回用例
          properties:
            code:
              type: number
              description: 状态号
              default: 200
            data:
              type: array
              description: 模糊查询拖车ID所返回的列表
              items:
                type: string
              default: ["1":0, "11":1]
    """

    return get_trailer_status_service(trailer_ID)


# 模糊查询获取拖车名列表
@trailer_bp.route('/trailer/getTrailerName', methods=["GET"])
def get_trailer_name():
    print(request.args.get("trailer_ID"))
    return get_trailer_name_service(request.args.get("trailer_ID"))


@trailer_bp.route('/trailer/updateStatus', methods=["POST"])
def update_trailer_status():
    """
       修改拖车状态的api
       ---
       tags:
         - 有关拖车的API
       parameters:
         - name: trailer_ID
           in: POST
           type: number string
           required: true
           description: 拖车ID
         - name: state
           in: POST
           type: int
           required: true
           description: 拖车需要更新的状态

       responses:
         500:
           description: 未查找到相关ID的拖车
         200:
           description: 更新成功

    """
    data = json.loads(request.get_data())
    return update_trailer_status_service(data)


@trailer_bp.route('/trailer/getAllTrailers', methods=["GET"])
def get_all_trailers():
    return deal_cors(jsonify({"code": 200, "data": get_all_trailer_id()}))